package com.dlc.shop.user.common.listener;

import com.dlc.shop.bean.enums.JumpType;
import com.dlc.shop.bean.enums.ProdStatusEnums;
import com.dlc.shop.bean.enums.ShopStatus;
import com.dlc.shop.bean.event.*;
import com.dlc.shop.bean.model.Product;
import com.dlc.shop.bean.order.GeneralActivitiesOrder;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.user.common.service.CostPerPopupService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Objects;

/**
 * 弹窗监听
 * @author track
 */
@Component("popupListener")
@AllArgsConstructor
public class PopupListener {
    private final CostPerPopupService costPerPopupService;

    @EventListener(PopupDeleteLinkEvent.class)
    public void deleteLink(PopupDeleteLinkEvent event) {
        costPerPopupService.deleteLink(event.getLinkId(), event.getShopId(), event.getType(), event.getCategoryIds());
    }

    @EventListener(PopupOfflineShopLinkEvent.class)
    @Transactional(rollbackFor = Exception.class)
    public void offlineShop(PopupOfflineShopLinkEvent event) {
        costPerPopupService.offlineShop(event.getShopIds());
        for (Long shopId : event.getShopIds()) {
            costPerPopupService.deleteLink(shopId, Constant.PLATFORM_SHOP_ID, JumpType.SHOP.value(), null);
        }
    }

    @EventListener(PopupDeleteCouponLinkEvent.class)
    public void deleteLinkBatch(PopupDeleteCouponLinkEvent event) {
        costPerPopupService.deleteLinkBatch(event.getCouponDtoList());
    }

    @EventListener(ProdChangeStatusEvent.class)
    @Order(GeneralActivitiesOrder.POPUP)
    public void prodChangeEvent(ProdChangeStatusEvent event) {
        // 如果是商品上线，就不管他了
        if (Objects.equals(ProdStatusEnums.NORMAL.getValue(), event.getStatus())) {
            return;
        }

        // 平台审核
        if (Objects.equals(ProdStatusEnums.PLATFORM_AUDIT.getValue(), event.getStatus())) {
            return;
        }
        Product product = event.getProduct();
        costPerPopupService.deleteLink(product.getProdId(), product.getShopId(), JumpType.PROD.value(), null);
    }

    @EventListener(ProdChangeEvent.class)
    @Order(GeneralActivitiesOrder.POPUP)
    public void prodChangeEvent(ProdChangeEvent event) {
        // 商品删除
        Product product = event.getProduct();
        costPerPopupService.deleteLink(product.getProdId(), product.getShopId(), JumpType.PROD.value(), null);
    }

    @EventListener(ShopChangeStatusEvent.class)
    @Order(GeneralActivitiesOrder.POPUP)
    public void popupShopChangeStatusListener(ShopChangeStatusEvent event) {
        // 店铺下架
        Long shopId = event.getShopId();
        ShopStatus shopStatus = event.getShopStatus();
        if (Objects.equals(shopStatus.value(), ShopStatus.OFFLINE.value())) {
            costPerPopupService.offlineShop(Collections.singletonList(shopId));
            costPerPopupService.deleteLink(shopId, Constant.PLATFORM_SHOP_ID, JumpType.SHOP.value(), null);
        }
    }
}
